﻿using CPPEI.Coolzon.Event.NCL;
using CPPEI.Coolzon.Event.UI;
using CPPEI.Coolzon.ServiceProxy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.UI.Views
{
    /// <summary>
    /// Interaction logic for LoginView.xaml
    /// 用于做登录验证
    /// </summary>
    public partial class LoginView : View
    {
        /// <summary>
        /// 用户的账号
        /// </summary>
        public string UserAccount { get; set; }
        /// <summary>
        /// 用户的密码
        /// </summary>
        public string UserPassword { get; set; }

        /// <summary>
        /// 登陆类型
        /// </summary>
        public string SolutionSelectionMode { get; set; }

        public LoginView()
        {
            InitializeComponent();

            _ctlBorderLogin.Visibility = System.Windows.Visibility.Visible;
            _ctlBorderIndicator.Visibility = System.Windows.Visibility.Hidden;

            NCLEventBus.Register(this);

            //判断是否是全屏
            if (UIContext.Current.IsFullScreen)
            {
                _fullScreenImage.Content = "还原";
            }
            else
            {
                _fullScreenImage.Content = "全屏";
            }
        }

        public LoginView(string userAccount, string userPassword)
        {
            InitializeComponent();

            _ctlEditAccount.Text = userAccount;
            _ctlEditPassword.Password = UserPassword;
            _ctlBorderLogin.Visibility = System.Windows.Visibility.Hidden;
            _ctlBorderIndicator.Visibility = System.Windows.Visibility.Hidden;

            NCLEventBus.Register(this);

            ctlBtnOK_Click(null, null);
            //判断是否是全屏
            if (UIContext.Current.IsFullScreen)
            {
                _fullScreenImage.Content = "还原";
            }
            else
            {
                _fullScreenImage.Content = "全屏";
            }
        }

        protected override void OnActive()
        {
            base.OnActive();

            // 如果有打开的方案信息，需要再这里释放占用的资源
            // 回收资源
            if (UI.UIContext.Current.RenderView != null)
            {
                // 1.去除RenderView里的所有组件的引用
                this.Dispatcher.Invoke(() =>
                {
                    (UI.UIContext.Current.RenderView as RenderView).ClearAllParts();
                });
                // 2.去除组件创建缓存中的组件引用
                PartInstanceCache.ClearAllParts();
            }
            //GC.Collect();
        }

        protected override void OnDeactive()
        {
            NCLEventBus.Unregister(this);

            base.OnDeactive();
        }

        protected override void OnClose()
        {
            NCLEventBus.Unregister(this);
            base.OnClose();
        }

        /// <summary>
        /// 点击登录按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctlBtnOK_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(_ctlEditAccount.Text))
            {
                return;
            }
            
            _ctlBorderLogin.Visibility = System.Windows.Visibility.Hidden;
            _ctlBorderIndicator.Visibility = System.Windows.Visibility.Visible;

            UserAccount = _ctlEditAccount.Text;
            UserPassword = _ctlEditPassword.Password;
            
            // 登录到系统
            // 构造登录请求
            NCLLoginEvent @event = new NCLLoginEvent();
            @event.Sender = this;
            @event.StartupPath = UIContext.Current.AppStartupPath;
            @event.AppType = UIContext.Current.AppType;
            @event.AppVersion = UIContext.Current.AppVersion;
            @event.OSVersion = UIContext.Current.OSVersion;
            @event.DotNetVersion = UIContext.Current.DotNetVersion;
            @event.UserAccount = UserAccount;
            @event.SolutionSelectionMode = SolutionSelectionMode;
            @event.UserPassword = UserPassword;

            NCLEventBus.PublishAsync(@event);
        }

        private void ctlBtnCancel_Click(object sender, RoutedEventArgs e)
        {
            //GC.Collect();
            NCLEventBus.Unregister(this);
            this.Cancel();
            Application.Current.Shutdown(); 
        }

        /// <summary>
        /// 全屏或退出全屏
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _fullScreenImage_Click(object sender, RoutedEventArgs e)
        {
            UIEventBus.Publish(new UIChangeFullScreenStateEvent());
            if (UIContext.Current.IsFullScreen)
            {
                _fullScreenImage.Content = "还原";
            }
            else
            {
                _fullScreenImage.Content = "全屏";
            }
        }

        #region NCL Event

        [Common.Subscribe]
        public void OnReceiveNCLLoginEventEvent(NCLLoginEvent @event)
        {
            this.Dispatcher.BeginInvoke(new Action<NCLLoginEvent>(OnInvokeReceiveNCLLoginEventEvent), @event);
        }

        private void OnInvokeReceiveNCLLoginEventEvent(NCLLoginEvent @event)
        {
            if (@event.State == NCLEventState.Failure)
            {
                System.Windows.MessageBox.Show("登录失败，出现错误。",
                    "错误",
                    System.Windows.MessageBoxButton.OK,
                    System.Windows.MessageBoxImage.Error);

                _ctlBorderLogin.Visibility = System.Windows.Visibility.Visible;
                _ctlBorderIndicator.Visibility = System.Windows.Visibility.Hidden;
            }
            else if (@event.State == NCLEventState.Finished)
            {
                // 登录成功
                UIContext.Current.UserAccount = UserAccount;
                UIContext.Current.UserName = @event.Result.UserName;
                UIContext.Current.UserIdentity = @event.Result.UserIdentity;
                _ctlBorderLogin.Visibility = System.Windows.Visibility.Visible;
                _ctlBorderIndicator.Visibility = System.Windows.Visibility.Hidden;
                this.Submit();
            }
        }
        #endregion
    }
}
